<?php
session_start();
function check() {
	if (!$_SESSION["username"]) {
		$pageURL = 'http';
		if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";
		}
		$pageURL .= "://";
		if ($_SERVER["SERVER_PORT"] != "80") {
			$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
		}
		$_SESSION["returnurl"] = $pageURL;
		header('Location: account.php');
	}
}


include_once ("model/productModel.php");
include_once ("model/cartModel.php");
$cart = new cartModel();
$ProductID = $_REQUEST["ProductID"];
$Quantity = $_REQUEST['Quantity'];

function getBill()
{
    check();
	$productModel = new productModel();
	$products = array();

	if (!isset($_SESSION["products"]) || count($_SESSION["products"]) == 0) {
		
	} else {
		$ProductIDs = $_SESSION["products"];
		$total =0;
		foreach ($ProductIDs as $key => $value) {
			$product = $productModel -> selectProductDetail($key);
			$product = $product[0];
			$product[Quantity]=$value;
			$product[TotalPrice]=$value*$product[Price];
			$total = $total + $product[TotalPrice];
			array_push($products,$product);
		}
	}

	include 'view/billView.php';
}

if($_REQUEST["Submit"])
{
	
	$deliverDate = $_REQUEST["Date"];
	$deliveryAddress = $_REQUEST["Address"];
	$Account = $_REQUEST["Account"];
	$Payment = $_REQUEST["Payment"];
	$data = $_SESSION['products'];
	
	$cart->addBill($deliverDate, $deliveryAddress, $Account, $Payment,$data);
	
} else {

			if ($_REQUEST["do"]) {
				switch ($_REQUEST["do"]) {
					case 'add' :
						$cart -> add($ProductID, $Quantity);
						$xxx = $cart -> message;
						if ($_SESSION['products'] && $_SESSION['username']) {
							$cart = $_SESSION['products'];
							echo "<h3>Cart</h3>";
							$pr = 0;
							$qu = 0;
							foreach ($cart as $key => $value) {
								$pr++;
								$qu = $qu + $value;
							}
							echo "No.Product: $pr <br/>
			                      Total No.Quantity: $qu <br/>
			                        ";
						}
			
						echo $xxx . '<br/>';
						echo '<a class="button" href="Cart.php"><span>View Cart</span></a>';
			
						break;
					case 'remove' :
						$cart -> remove($ProductID);
									$xxx = $cart -> message;
						if ($_SESSION['products'] && $_SESSION['username']) {
							$cart = $_SESSION['products'];
							echo "<h3>Cart</h3>";
							$pr = 0;
							$qu = 0;
							foreach ($cart as $key => $value) {
								$pr++;
								$qu = $qu + $value;
							}
							echo "No.Product: $pr <br/>
			                      Total No.Quantity: $qu <br/>
			                        ";
						}
			
						echo $xxx . '<br/>';
						echo '<a class="button" href="Cart.php"><span>View Cart</span></a>';
						
						break;
					case 'bill' :
						getBill();
						break;
			
					default :
						break;
				}
			} else {
			check();
				$productModel = new productModel();
				$products = array();
			
				if (!isset($_SESSION["products"]) || count($_SESSION["products"]) == 0) {
					
				} else {
					$ProductIDs = $_SESSION["products"];
					foreach ($ProductIDs as $key => $value) {
						$product = $productModel -> selectProductDetail($key);
						$product = $product[0];
						$product[Quantity]=$value;
						$product[TotalPrice]=$value*$product[Price];
						
						array_push($products,$product);
					}
				}
			
			if($_REQUEST["mess"])
			{
				$mess= $_REQUEST["mess"];
			}
				include 'view/cartDetail.php';
			}
}
?>